package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import artid.com.arttogoa.fragment.TransactionManagement_Fragment;
import artid.com.arttogoa.volley.VolleyBasic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：TransactionManagement_Activity   
 * 类描述   ：交易管理页面   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-18 上午11:38:06   
 *
 */
public class TransactionManagement_Activity extends BaseActivity implements OnClickListener{
	private Context mContext;
	private RadioGroup mRadioGroup;
	private RadioButton allRB,toBeShippedRB,pendingPaymentRB,alreadyShippedRB,alreadySoldRB,alreadyClosedRB;
	private ViewPager mViewPager;
	private List<Fragment> fragementlist;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transactionmanagement);
		setContent(this, getIntent());
		mContext=TransactionManagement_Activity.this;
		setContent(this, getIntent());
		initView();
	}

	private void initView(){
		VolleyBasic.showLoding(mContext);
		mTittle.setText("交易管理");
		setBack();
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.TransactionManagement_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		mRadioGroup = (RadioGroup) findViewById(R.id.transactionManagement_RadioGroup);
		// 得到RadioGroup中的RadioButton按钮
		allRB=(RadioButton) findViewById(R.id.transactionManagement_allRB);
		allRB.setOnClickListener(this);
		toBeShippedRB=(RadioButton) findViewById(R.id.transactionManagement_toBeShippedRB);
		toBeShippedRB.setOnClickListener(this);
		pendingPaymentRB=(RadioButton) findViewById(R.id.transactionManagement_pendingPaymentRB);
		pendingPaymentRB.setOnClickListener(this);
		alreadyShippedRB=(RadioButton) findViewById(R.id.transactionManagement_alreadyShippedRB);
		alreadyShippedRB.setOnClickListener(this);
		alreadySoldRB=(RadioButton) findViewById(R.id.transactionManagement_alreadySoldRB);
		alreadySoldRB.setOnClickListener(this);
		alreadyClosedRB=(RadioButton) findViewById(R.id.transactionManagement_alreadyClosedRB);
		alreadyClosedRB.setOnClickListener(this);
		mViewPager = (ViewPager) findViewById(R.id.transactionManagement_pager);
		fragementlist = new ArrayList<Fragment>();
		TransactionManagement_Fragment Fragment0 = new TransactionManagement_Fragment(0);
		TransactionManagement_Fragment Fragment1 = new TransactionManagement_Fragment(1);
		TransactionManagement_Fragment Fragment2 = new TransactionManagement_Fragment(2);
		TransactionManagement_Fragment Fragment3 = new TransactionManagement_Fragment(3);
		TransactionManagement_Fragment Fragment4 = new TransactionManagement_Fragment(4);
		TransactionManagement_Fragment Fragment5 = new TransactionManagement_Fragment(5);
		fragementlist.add(Fragment0);
		fragementlist.add(Fragment1);
		fragementlist.add(Fragment2);
		fragementlist.add(Fragment3);
		fragementlist.add(Fragment4);
		fragementlist.add(Fragment5);
		mViewPager.setAdapter(new MyPageadapter(getSupportFragmentManager()));
		// 实现滑动 绑定监听（滑动页面并改变RadioButton的改变）
		mViewPager.setOffscreenPageLimit(0);
		mViewPager.setOnPageChangeListener(onPage);
		//判断是否显示其他页面
		if(getIntent().getBooleanExtra("IsShowOtherActivity", false)==true){
			mViewPager.setCurrentItem(getIntent().getIntExtra("whichActivity", 0));
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.transactionManagement_allRB:
			//全部
			mViewPager.setCurrentItem(0);
			break;
		case R.id.transactionManagement_toBeShippedRB:
			//代发货
			mViewPager.setCurrentItem(1);
			break;
		case R.id.transactionManagement_pendingPaymentRB:
			//买家待付款
			mViewPager.setCurrentItem(2);
			break;
		case R.id.transactionManagement_alreadyShippedRB:
			//已发货
			mViewPager.setCurrentItem(3);
			break;
		case R.id.transactionManagement_alreadySoldRB:
			//已出售
			mViewPager.setCurrentItem(4);
			break;
		case R.id.transactionManagement_alreadyClosedRB:
			//已关闭
			mViewPager.setCurrentItem(5);
			break;

		default:
			break;
		}
	}

	// 实现滑动
	class MyPageadapter extends FragmentPagerAdapter {

		public MyPageadapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragementlist.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragementlist.size();
		}
	}

	OnPageChangeListener onPage=new OnPageChangeListener(){

		// 滑动页面并改变RadioButton的改变
		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			if(position==0){
				allRB.setChecked(true);
			}else if(position==1){
				toBeShippedRB.setChecked(true);
			}else if(position==2){
				pendingPaymentRB.setChecked(true);
			}else if(position==3){
				alreadyShippedRB.setChecked(true);
			}else if(position==4){
				alreadySoldRB.setChecked(true);
			}else if(position==5){
				alreadyClosedRB.setChecked(true);
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

	};
	
	//广播接收器
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override  
		public void onReceive(Context context, Intent intent) {  
			String Intent_type=intent.getStringExtra("Intent_type");
			if("我要发货".equals(Intent_type)){
				mViewPager.setCurrentItem(3);
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}
}
